<template>
	<view class="box">
		<!-- 标签栏 -->
		<view class="tab">
			<u-tabs :list="list1" @click="click" lineColor="#109C7A" :current="active" :activeStyle="{
					color: '#333',
					fontSize: '32rpx',
					fontWeight: 'bold'
				}" :inactiveStyle="{
					color: '#999',
					fontSize: '32rpx',
				}"></u-tabs>
		</view>
		<!-- 空 -->
		<view class="none" v-if="!couponList.length">
			<view class="none-img f-w">
				<image src="https://jw.xzsw2021.com/static/big/icon_222@2x.png" mode=""></image>
			</view>
			<view class="f30 tr c666">暂时没有任何优惠券哦</view>
		</view>
		<!-- 列表 -->
		<view class="list" v-else>
			<template v-if="active==0">
				<view class="card flex" v-for="(item,index) of couponList" :key="index">
					<!-- <view class="tag">{{day(item)}}</view> -->
					<view class="tag">
						{{getEndDay(item)}}
						<!-- <u-count-down :time="214 * 60 * 60 * 1000" format="DD:HH:mm:ss"></u-count-down>#@# -->
					</view>
					<view class="left">
						<view class="price">￥<text class="f40 bold">{{item.coupon_price}}</text></view>
						<view class="tj" v-if="item.use_min_price > 0">满{{item.use_min_price}}可用</view>
						<view class="tj" v-else>无门槛</view>
					</view>
					<view class="right">
						<view class="one">{{item.coupon_title}}</view>
						<view class="two f24">{{item.start_time.slice(0,10)}} - {{item.end_time.slice(0,10)}}</view>
						<view class="three flex">
							<view>
								<view class="desc" :class="!item.look?'hide-line':''" v-if="item.coupon.remarks">
									{{item.coupon.remarks}}
								</view>
								<view class="desc" :class="!item.look?'hide-line':''" v-else></view>
							</view>
							<view class="icon" @click="zhankai(index)" v-if="item.desc.length>17">
								<image src="https://jw.xzsw2021.com/static/img/icon_62@2x.png" mode=""
									v-if="!item.look"></image>
								<image src="https://jw.xzsw2021.com/static/img/icon_63@2x.png" mode="" v-else>
								</image>
							</view>
						</view>
						<!-- 立即领取 -->
						<view class="lingqu" @click="lingqu(item)" v-if="active==0">
							去使用
						</view>
					</view>
				</view>
			</template>
			<template v-if="active==1">
				<view class="card flex" v-for="(item,index) of couponList" :key="index">
					<view class="tag tag2" v-if="item.status == 2">已失效</view>
					<view class="tag tag2" v-else>已使用</view>
					<view class="left left2">
						<view class="price">￥<text class="f40">{{item.coupon_price}}</text></view>
						<view class="tj" v-if="item.use_min_price > 0">满{{item.use_min_price}}可用</view>
						<view class="tj" v-else>无门槛</view>
					</view>
					<view class="right right2">
						<view class="one">{{item.title}}</view>
						<view class="two">{{item.start_time.slice(0,10)}} - {{item.end_time.slice(0,10)}}</view>
						<view class="three flex">
							<view>
								<view class="desc" :class="!item.look?'hide-line':''" v-if="item.coupon.remarks">
									{{item.coupon.remarks}}
								</view>
								<view class="desc" :class="!item.look?'hide-line':''" v-else>商家发放</view>
							</view>
							<view class="icon" @click="zhankai(index)" v-if="item.coupon.remarks>17">
								<image src="https://jw.xzsw2021.com/static/img/icon_62@2x.png" mode=""
									v-if="!item.look"></image>
								<image src="https://jw.xzsw2021.com/static/img/icon_63@2x.png" mode="" v-else>
								</image>
							</view>
						</view>
					</view>
				</view>
			</template>
			<!-- <u-loadmore :status="status" v-if="status" /> -->
		</view>
	</view>
</template>

<script>
	import {
		getUserCouponListApi
	} from '@/api/coupon.js'
	export default {
		data() {
			return {
				active: 0,
				search: {
					statusTag: 1,
					type_2: 3,
					page: 1,
					limit: 10
				},
				status: '',
				couponList: [], //优惠券list
				list1: [{
					name: '可使用',
				}, {
					name: '已失效',
				}], //标签
			}
		},
		mounted() {
			this.getCouponList()
		},
		onReachBottom: function() {
			this.search.page += 1
			this.getCouponList()
		},
		methods: {
			getEndDay(item) {
				if (!item.end_time) {
					return '永久'
				}
				let endTime = new Date(item.end_time).getTime() / 1000 - parseInt(new Date().getTime() / 1000);
				let timeDay = parseInt(endTime / 60 / 60 / 24); //相差天数
				endTime = endTime - timeDay * 60 * 60 * 24;
				let timeHour = parseInt(endTime / 60 / 60); //相差小时
				if (timeDay) {
					return `剩${timeDay}天`
				} else {
					return `剩${timeHour}时`
				}
			},
			getCouponList() {
				this.status = 'loading'
				getUserCouponListApi(this.search).then(res => {
					if (this.search.page == 1) {
						this.couponList = res.data.list
					} else {
						this.couponList.push(...res.data.list)
					}
					if (this.couponList.length < this.search.limit) {
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}

				}).catch(err => {
					console.log(err)
				})
			},
			//切换导航栏 
			click(item) {
				this.active = item.index
				if (item.index == 1) {
					this.search.statusTag = 2
				} else {
					this.search.statusTag = 1
				}
				this.search.page = 1
				this.getCouponList()
			},
			// 展开
			zhankai(index) {
				this.list[index].look = !this.list[index].look;
				console.log("????")
			},
			//领取
			lingqu(item) {
				console.log(item)
				if (item.coupon.type == 1) {
					uni.navigateTo({
						url: "/pages/client/shop/shop-detail/shop-detail?id=" + item.coupon.product[0].product_id
					})
				} else {
					uni.navigateTo({
						url: "/pages/client/shop/shop?id=" + item.mer_id
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		.tab {
			height: 96rpx;
			padding: 0 100rpx;
		}

		.none {
			.none-img {
				margin: 148rpx auto 80rpx;
				width: 376rpx;
				height: 329rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.list {
			min-height: calc(100vh - 96rpx);
			background-color: #F5F5F5;
			padding: 20rpx 30rpx;

			.card {
				position: relative;
				margin-bottom: 20rpx;
				width: 690rpx;
				// height: 186rpx;
				padding: 23rpx 28rpx 23rpx 0;
				background: #FFFFFF;
				border-radius: 10rpx;
				box-sizing: border-box;

				.left {
					flex-shrink: 0;
					width: 160rpx;
					height: 160rpx;
					border-right: 1rpx dashed #CCCCCC;
					text-align: center;
					color: #F83535;

					.price {
						margin-top: 60rpx;
					}

					.tj {
						margin-top: 6rpx;
						font-size: 24rpx;
						color: #666;
					}
				}

				.left2 {
					color: #D1D1D1;

					.tj {
						color: #D1D1D1;
					}
				}

				.right {
					position: relative;
					flex: 1;
					padding-right: 30rpx;
					margin-left: 30rpx;
					font-size: 28rpx;

					.two {
						margin: 28rpx 0;
						color: #666;
					}

					.three {
						color: #999;
						font-size: 24rpx;
						overflow: hidden;

						.desc {
							flex: 1;
							width: 420rpx;
						}

						.icon {
							flex-shrink: 0;
							margin-left: 20rpx;
							width: 26rpx;
							height: 14rpx;

							image {
								width: 100%;
								height: 100%;
							}
						}
					}

					.lingqu {
						position: absolute;
						right: 30rpx;
						top: 60rpx;
						width: 130rpx;
						height: 44rpx;
						line-height: 44rpx;
						text-align: center;
						background: #FFFFFF;
						border: 1rpx solid #F83535;
						border-radius: 22rpx;
						font-size: 26rpx;
						color: #F83535;
					}

					.get {
						border: 1rpx solid #ccc;
						color: #666;
					}
				}

				.right2 {
					color: #D1D1D1;

					.two,
					.three {
						color: #D1D1D1;
					}
				}

				.tag {
					position: absolute;
					left: 0;
					top: 0;
					width: 100rpx;
					height: 40rpx;
					line-height: 40rpx;
					font-size: 24rpx;
					text-align: center;
					color: #fff;
					font-weight: bold;
					background: linear-gradient(85deg, #FF9200 0%, #FFBC00 100%);
					border-radius: 10rpx 0rpx 10rpx 0rpx;
				}

				.tag2 {
					background: #DBDBDB;
				}
			}
		}
	}
</style>
